﻿#region Using

using System;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using Onyx;

#endregion

namespace onyxLauncher
{
    public partial class SplashWindow : Form
    {
        #region Members

        internal static visible on;
        private ConsoleWindow con;
        //Image splash = Image.FromFile(Application.StartupPath + "\\splash.png");

        #region Nested type: structBlend

        private struct structBlend
        {
            public byte Alpha;
            public byte AlphaFormat;
            public byte BlendOp;
            public byte Flags;
        }

        #endregion

        #region Nested type: visible

        internal delegate void visible(bool show);

        #endregion

        #endregion

        #region PInvoke

        [DllImport("GDI32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        private static extern IntPtr CreateCompatibleDC(IntPtr hDC);

        [DllImport("USER32.DLL", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        private static extern IntPtr GetDC(IntPtr handle);

        [DllImport("GDI32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        private static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);

        [DllImport("USER32.DLL", EntryPoint = "SetWindowLongA", CharSet = CharSet.Ansi, SetLastError = true,
            ExactSpelling = true)]
        private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dsNewLong);

        [DllImport("USER32.DLL", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        private static extern bool UpdateLayeredWindow(IntPtr handle, IntPtr hdcDst, ref Point pptDst, ref Size psize,
                                                       IntPtr hdcSrc, ref Point pprSrc, int crKey,
                                                       ref structBlend pblend, int dwFlags);

        #endregion

        #region Constructor

        public SplashWindow()
        {
            on = new visible(On);
            InitializeComponent();
            var bitmap = new Bitmap("splash.png");

            int nIndex = -20;
            int dsNewLong = 0x80000;
            SetBounds(0, 0, bitmap.Width, bitmap.Height, BoundsSpecified.Size);
            SetWindowLong(Handle, nIndex, dsNewLong);
            PNG_Aply(this, bitmap);
            bitmap.Dispose();

            //this.Width = splash.Width;
            //this.Height = splash.Height;
            //this.BackgroundImage = splash;
            con = new ConsoleWindow();
        }

        #endregion

        #region Events

        private void On(bool show)
        {
            Visible = show;
        }

        private void SplashWindow_Shown(object sender, EventArgs e)
        {
            //MessageBox.Show("done");
            Application.DoEvents();
            Thread.Sleep(500);
            con.print("Launcher giving controll");
            Application.DoEvents();
            sys.engineInit(con, Process.GetCurrentProcess().Handle, "+set fs_game baseq3");
        }

        #endregion

        #region Procedures

        private void PNG_Aply(Form form_win, Bitmap bit_png)
        {
            IntPtr dC = GetDC(form_win.Handle);
            IntPtr hDC = CreateCompatibleDC(dC);
            structBlend pblend = new structBlend();
            pblend.BlendOp = 0;
            pblend.Flags = 0;
            pblend.Alpha = 0xff;
            pblend.AlphaFormat = 1;
            SelectObject(hDC, bit_png.GetHbitmap(Color.Black));
            Point pptDst = new Point(form_win.Left, form_win.Top);
            Size psize = new Size(bit_png.Width, bit_png.Height);
            Point pprSrc = new Point(0, 0);
            UpdateLayeredWindow(form_win.Handle, dC, ref pptDst, ref psize, hDC, ref pprSrc, 0, ref pblend, 2);
        }

        #endregion
    }
}